﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Security.Permissions;

namespace HyperCrunch.Druid.Mobile
{
    public abstract class AppModelBase
    {
        public ModuleInfo Module { get; set; }
        public UIContext AppContext { get; set; }
        public string Stylesheet { get; set; }
        public string Script { get; set; }

        public bool CanEdit
        {
            get
            {
                return ModulePermissionController.CanEditModuleContent(Module);
            }
        }
        
        public string HelperScript
        {
            // TODO: Make this more flexible and extract to file
            get
            {
                return "<script type=\"text/javascript\">" +
                       "function displayMessage(message, duration) {" +
                       "if (!duration) duration = 3000;" +
                        "$(\"<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'><h1>\" + message + \"</h1></div>\")" +
                        ".css({ 'display': 'block', 'opacity': 0.96, 'top': $(window).scrollTop() + 100 })" +
                        ".appendTo($.mobile.pageContainer)" +
                        ".delay(1500)" +
                        ".fadeOut(1000, function () {"+
                        "    $(this).remove();" +
                        "});" +
                        "}" +
                        "</script>";
            }
        }

        public string RouteUrl(string route)
        {
            return Utility.RouteUrl(AppContext, route);
        }

        /// <summary>
        /// URL of the current page
        /// </summary>
        public string PageUrl
        {
            get
            {
                return Utility.NavigateUrl(AppContext, new string[] { }, new List<string> { });
            }
        }

        public string DetailUrl(string id)
        {
            return Utility.NavigateUrl(AppContext, new string[] { "id=" + id}, 
                        new List<string>{ "id" });
        }

        public string ListUrl
        {
            get
            {
                return Utility.NavigateUrl(AppContext, new string[]{},
                            new List<string> { "id" });
            }
        }

        /// <summary>
        /// URL to use for asset references in same folder as view script
        /// </summary>
        public string AssetUrl
        {
            get
            {
                if (string.IsNullOrEmpty(Script))
                    return "";
                else
                    return Script.Substring(0, Script.LastIndexOf("/"));
            }
        }

        /// <summary>
        /// URL for returning back to the view rendered by SiteRenderer
        /// </summary>
        public string ReturnUrl
        {
            get
            {
                return Utility.ReturnUrl(AppContext);
            }
        }
    }
}
